package com.ruicar.afs.cloud.afscase.loanapprove.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.backtopartnersinfo.service.CaseBackToPartnersInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.LoanBackReasonCondition;
import com.ruicar.afs.cloud.afscase.backtopartnersinfo.entity.CaseBackToPartnersInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.LoanBackReasonListCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CaseBackReasonInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CaseBackReasonInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.enums.FileStatusEnum;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.service.ComAttachmentManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author rongji.zhang
 * @Date 2020/8/7
 * @description
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/loanBackReason")
@Api("退回经销商配置")
public class LoanBackReasonController {

    private CaseBackToPartnersInfoService caseBackToPartnersInfoService;
    private ComAttachmentManagementService comAttachmentManagementService;
    private CaseBackReasonInfoService caseBackReasonInfoService;
    private ComAttachmentFileService comAttachmentFileService;

    @PostMapping("/getBackPartnersList")
    @ApiOperation(value = "获取退回原因列表")
    public IResponse getBackPartnersList(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        List<CaseBackToPartnersInfo> pageResult = caseBackToPartnersInfoService.list(Wrappers.<CaseBackToPartnersInfo>query().lambda()
                .eq(StringUtils.isNotBlank(loanBackReasonCondition.getContractNo()),CaseBackToPartnersInfo::getContractNo,loanBackReasonCondition.getContractNo())
                .eq(CaseBackToPartnersInfo::getTaskId,loanBackReasonCondition.getTaskId())
                .orderByAsc(CaseBackToPartnersInfo::getCreateTime));
        return IResponse.success(null != pageResult ? pageResult : "");
    }

    @PostMapping("/getFileTypeList")
    @ApiOperation(value = "获取退回原因文件类型列表")
    public IResponse getFileTypeList(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        List<CaseBackReasonInfo> resultList= caseBackReasonInfoService.getFileTypeList();
        return IResponse.success(resultList);
    }

    @PostMapping("/getBackWords")
    @ApiOperation(value = "获取所有退回话术列表")
    public IResponse getBackWords(@ModelAttribute CaseBackReasonInfo caseBackReasonInfo) {
        List<CaseBackReasonInfo> resultList= caseBackReasonInfoService.list(Wrappers.<CaseBackReasonInfo>query().lambda()
                .eq(CaseBackReasonInfo::getAttachmentId, caseBackReasonInfo.getAttachmentId()));
        return IResponse.success(resultList);
    }

    @PostMapping("/editBackPartner")
    @ApiOperation(value = "修改退回原因")
    public IResponse editBackPartner(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        caseBackToPartnersInfoService.editBackPartner(loanBackReasonCondition);
        return IResponse.success(true);
    }

    @PostMapping("/saveBackPartner")
    @ApiOperation(value = "新增退回原因")
    public IResponse saveBackPartner(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        caseBackToPartnersInfoService.saveBackPartner(loanBackReasonCondition);
        return IResponse.success(true);
    }

    @PostMapping("/deleteBackPartner")
    @ApiOperation(value = "删除退回原因")
    public IResponse deleteBackPartner(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        caseBackToPartnersInfoService.deleteBackPartner(loanBackReasonCondition);
        return IResponse.success(true);
    }

    @PostMapping("/getBackReasonList")
    @ApiOperation(value = "获取退回描述列表")
    public IResponse getBackReasonList(@ModelAttribute LoanBackReasonCondition loanBackReasonCondition) {
        IPage<CaseBackToPartnersInfo> pageResult = caseBackToPartnersInfoService.page(new Page(loanBackReasonCondition.getPageNumber(),loanBackReasonCondition.getPageSize()), Wrappers.<CaseBackToPartnersInfo>query().lambda()
                .eq(StringUtils.isNotBlank(loanBackReasonCondition.getContractNo()),CaseBackToPartnersInfo::getContractNo,loanBackReasonCondition.getContractNo())
                .eq(StringUtils.isNotBlank(loanBackReasonCondition.getApplyNo()),CaseBackToPartnersInfo::getApplyNo,loanBackReasonCondition.getApplyNo())
                .eq(CaseBackToPartnersInfo::getTaskId,loanBackReasonCondition.getTaskId()));
        return IResponse.success(null != pageResult ? pageResult : "");
    }

    @PostMapping("/getBackDescList")
    @ApiOperation(value = "获取退回话术列表")
    public IResponse getBackDescList(@RequestParam("data") String jsonDataArrStr) {
        String[] str = jsonDataArrStr.split(",");
        List resultList = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            List<CaseBackReasonInfo> attachmentList = caseBackReasonInfoService.list(Wrappers.<CaseBackReasonInfo>query().lambda()
                    .eq(CaseBackReasonInfo::getAttachmentId, str[i])
                    .orderByAsc(CaseBackReasonInfo::getCreateTime));
            resultList.add(attachmentList);
        }
        return IResponse.success(resultList);
    }

    @PostMapping("/saveOrUpdateBackResonList")
    @ApiOperation(value = "批量新增或修改退回原因")
    public IResponse getBackDescList(@RequestBody List<LoanBackReasonCondition> loanBackReasonConditionList) {
        loanBackReasonConditionList.forEach(loanBackReasonCondition -> {
            if(loanBackReasonCondition.getId()!=null){
                caseBackToPartnersInfoService.editBackPartner(loanBackReasonCondition);
            } else {
                caseBackToPartnersInfoService.saveBackPartner(loanBackReasonCondition);
            }
        });
        return IResponse.success(true);
    }
}
